//
//  LHNewFeatureViewController.swift
//  sinaWeibo
//
//  Created by LiHong on 15/10/30.
//  Copyright © 2015年 LiHong. All rights reserved.
//

import UIKit

private let reuseIdentifer = "Cell"

class LHNewFeatureViewController: UICollectionViewController {

    // MARK: - 属性
    private let itemCount = 4
    
    private var layout = UICollectionViewFlowLayout()
    
    init() {
        super.init(collectionViewLayout: layout)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
      //  self.collectionViewLayout.registerClass(LHNewFeatureViewCell.self, forDecorationViewOfKind: reuseIdentifer)
        self.collectionView?.registerClass(LHNewFeatureViewCell.self, forCellWithReuseIdentifier: reuseIdentifer)
        prepareLayout()
    }
    
    func prepareLayout() {
        layout.itemSize = UIScreen.mainScreen().bounds.size
        layout.minimumLineSpacing = 0
        layout.minimumInteritemSpacing = 0
        layout.scrollDirection = UICollectionViewScrollDirection.Horizontal
        
        collectionView?.pagingEnabled = true
        collectionView?.bounces = false
        
    }
    
    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return itemCount
    }

    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifer, forIndexPath: indexPath) as! LHNewFeatureViewCell
        cell.imageIndex = indexPath.item
        return cell
    }
    
    override func collectionView(collectionView: UICollectionView, didEndDisplayingCell cell: UICollectionViewCell, forItemAtIndexPath indexPath: NSIndexPath) {
        /// 正在显示的cell的下标
        let showIndexPath = collectionView.indexPathsForVisibleItems().first!
        
        let cell = collectionView.cellForItemAtIndexPath(showIndexPath) as! LHNewFeatureViewCell
        
        if showIndexPath.item == itemCount - 1 {
            cell.startButtonAnimation()
        }
    }

}

class LHNewFeatureViewCell: UICollectionViewCell {
    var imageIndex: Int = 0 {
        didSet{
            backgroundImageView.image = UIImage(named: "new_feature_\(imageIndex + 1)")
            startButton.hidden = true
        }
    }
    // MARK: - 构造方法
    override init(frame: CGRect) {
        super.init(frame: frame)
        prepareUI()
    }

    func startButtonAnimation() {
        startButton.hidden = false
        startButton.transform = CGAffineTransformMakeScale(0, 0)
        
        UIView.animateWithDuration(1, delay: 0.1, usingSpringWithDamping: 0.5, initialSpringVelocity: 5, options: UIViewAnimationOptions(rawValue: 0), animations: { () -> Void in
                self.startButton.transform = CGAffineTransformIdentity
            }) { (_) -> Void in
                
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    // 布局UI
    private func prepareUI() {
        contentView.addSubview(backgroundImageView)
        contentView.addSubview(startButton)
        
        backgroundImageView.translatesAutoresizingMaskIntoConstraints = false
        startButton.translatesAutoresizingMaskIntoConstraints = false
        // 背景
        backgroundImageView.ff_Fill(contentView)
        
//        contentView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[bkg]-0-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["bkg": backgroundImageView]))
//        contentView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[bkg]-0-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["bkg": backgroundImageView]))
        
        // 开始按钮
        startButton.ff_AlignInner(type: ff_AlignType.BottomCenter, referView: contentView, size: nil, offset: CGPoint(x: 0, y: -160))
        
//        contentView.addConstraint(NSLayoutConstraint(item: startButton, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
//        contentView.addConstraint(NSLayoutConstraint(item: startButton, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: -160))
        
    }
    
    
    
    /// 开始按钮触发事件
    func startButtonClick() {
        (UIApplication.sharedApplication().delegate as! AppDelegate).switchRootController(true)
    }
    
    // MARK: - 懒加载
    /// 背景
    private lazy var backgroundImageView = UIImageView()
    
    /// 按钮
    private lazy var startButton: UIButton = {
        let button = UIButton()
        // 设置背景
        button.setBackgroundImage(UIImage(named: "new_feature_finish_button"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "new_feature_finish_button_highlighted"), forState: UIControlState.Highlighted)
        button.setTitle("开始体验", forState: UIControlState.Normal)
        
        button.addTarget(self, action: "startButtonClick", forControlEvents: UIControlEvents.TouchUpInside)
        
        return button
    }()
}
